/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.bar;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.RowSorter;
import javax.swing.SwingConstants;

import org.jxSwing.border.EtchedBorderFactory;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.bar
 * @date 22/03/2014 - 17:09:02
 */
public final class FooterTableBar extends JToolBar {
	
	private static final long serialVersionUID = 6320737246336504683L;
	
	private JLabel lblRows;
	
	private int rowsView = 0;
	
	private int rowsCount = 0;
	
	private final JTable table;
	
	/**
	 * @param table
	 */
	public FooterTableBar(final JTable table) {
	
		this.table = table;
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		setMargin(new Insets(2, 2, 2, 2));
		setBounds(new Rectangle(2, 2, 2, 2));
		setFloatable(false);
		setSize(new Dimension(400, 35));
		setMinimumSize(new Dimension(400, 35));
		setPreferredSize(new Dimension(400, 35));
		
		add(getLblRows());
		
		final RowSorter<?> rowSorter = getTable().getRowSorter();
		
		//
		if(rowSorter != null) {
			
			setRows(rowSorter.getViewRowCount(), rowSorter.getModelRowCount());
			
		}else {
			
			setRows(getTable().getRowCount(), getTable().getRowCount());
		}
	}
	
	/**
	 * @param rowsCount
	 */
	private void setRowsCount(final int rowsCount) {
	
		this.rowsCount = rowsCount;
	}
	
	/**
	 * @param rowsView
	 */
	private void setRowsView(final int rowsView) {
	
		this.rowsView = rowsView;
	}
	
	public JLabel getLblRows() {
	
		if(lblRows == null) {
			lblRows = new JLabel(" " + getRowsView() + "/" + getRowsCount() + " ");
			lblRows.setFont(new Font(lblRows.getFont().getName(), lblRows.getFont().getStyle(), 14));
			lblRows.setBorder(EtchedBorderFactory.createRaisedBorder());
			lblRows.setHorizontalTextPosition(SwingConstants.CENTER);
			lblRows.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblRows;
	}
	
	/**
	 * @return the rowsCount
	 */
	public int getRowsCount() {
	
		return rowsCount;
	}
	
	/**
	 * @return the rowsView
	 */
	public int getRowsView() {
	
		return rowsView;
	}
	
	/**
	 * @return the table
	 */
	public JTable getTable() {
	
		return table;
	}
	
	/**
	 * @param rowsView
	 * @param rowsCount
	 */
	public void setRows(final int rowsView, final int rowsCount) {
	
		setRowsView(rowsView);
		setRowsCount(rowsCount);
		
		getLblRows().setText(" " + rowsView + "/" + rowsCount + " ");
		getLblRows().repaint();
	}
}
